package com.power.model.factories
{
	import com.power.model.FloorVo;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class FloorFactory extends EventDispatcher
	{
		private var _config_url:String;
		
		private var _floors:Vector.<FloorVo> = new Vector.<FloorVo>();
		
		private var xmlloader:URLLoader;
		
		private var _floorxml:XML;
		
		private var _floortotalnum:int;
		
		private var _floorpicurls:Array = [];
		
		private var _floortypes:Array = [];
		
		private var _tempfloortype:String;
		
		private var _floorindex:int;
		
		private var _floorTagsUrl:Array =[];
		
		private var _floorimage:Bitmap;
		
		private var _floortagimage:Bitmap;
		
		private var _floorType:String;
		
		private var _tagNames:Array = [];
		
		private var _tagUrls:Array = [];
		
		private var _tempTagName:String;
		
		private var _tempTagUrl:String;
		
		public function FloorFactory()
		{
		}
		public function startLoadFloorAssets(floorurl:String,floorType:String):void
		{
			_config_url = floorurl;
			_floorType = floorType;
			xmlloader = new URLLoader();
			xmlloader.load(new URLRequest(_config_url));
			xmlloader.addEventListener(ProgressEvent.PROGRESS,loadfloorxmlProHandler);
			xmlloader.addEventListener(Event.COMPLETE,loadxmlendhandler);
		}
		private function loadfloorxmlProHandler(e:ProgressEvent):void
		{
			// indicator progress when start to loaded floor.xml file
		}
		private function loadxmlendhandler(e:Event):void
		{
			var tempxml:XML = new XML(e.target.data);
			startParseXml(tempxml);
		}
		private function startParseXml(xml:XML):void
		{
			_floorxml = xml;
			_floortotalnum = _floorxml.floor.length();
			for(var i:int = 0 ;i<_floortotalnum;i++)
			{
				var tempurl:String = _floorxml.floor[i].picurl;
				var temptype:String = _floorxml.floor[i].type;
				var temptag:String = _floorxml.floor[i].tag;
				var tempTagName:String = _floorxml.floor[i].tagName;
				var tempTagUrl:String = _floorxml.floor[i].tagUrl;
				
				if(_floorType==temptype){
					_floorpicurls.push(tempurl);
					_floortypes.push(temptype);
					_floorTagsUrl.push(temptag);
					_tagNames.push(tempTagName);
					_tagUrls.push(tempTagUrl);
				}
				if(_floorType=="全部地板"){
					_floorpicurls.push(tempurl);
					_floortypes.push(temptype);
					_floorTagsUrl.push(temptag);
					_tagNames.push(tempTagName);
					_tagUrls.push(tempTagUrl);
				}
					
			}
			loadFloorData();
		}
		private function loadFloorData():void
		{
			var tempurl:String  = _floorpicurls[_floorindex];
			_tempfloortype = _floortypes[_floorindex];
			_tempTagName = _tagNames[_floorindex];
			_tempTagUrl = _tagUrls[_floorindex];
			
			var temploader:Loader = new Loader();
			try{
				temploader.load(new URLRequest(tempurl));
				temploader.contentLoaderInfo.addEventListener(Event.COMPLETE,onloaderEndHandler);
			}catch(e:Error){
				
				throw new Error("the url of picture you improved is incorrect!");
			}
			
			
		}
		private function onloaderEndHandler(e:Event):void
		{
			_floorimage = new Bitmap(e.currentTarget.content.bitmapData);
			var tempurl:String = _floorTagsUrl[_floorindex];
			var tagloader:Loader = new Loader();
			tagloader.load(new URLRequest(tempurl));
			tagloader.contentLoaderInfo.addEventListener(Event.COMPLETE,onloaderTagEndHandler);
	
			
		}
		private function onloaderTagEndHandler(e:Event):void
		{
			_floortagimage = new Bitmap(e.currentTarget.content.bitmapData);
			var floordata:FloorVo = new FloorVo();
			floordata._image =_floorimage;
			floordata._tag = _floortagimage;
			floordata._type = _tempfloortype;
			floordata._tagName = _tempTagName;
			floordata._tagUrl = _tempTagUrl;
			_floors.push(floordata);
			_floorindex++;
			if(_floorindex>=_floorpicurls.length)
			{
				this.dispatchEvent(new Event("ParserFloorDataEnd",true));
				this.dispatchEvent(new Event("ParserFloorDataEndByType",true));
			}else
			{
				loadFloorData();
			}
			
		}
		public function getFloorData():Vector.<FloorVo>
		{
			return _floors;
		}
	}
}